/***********外部容器触屏事件指令*/
yjx.directive('containerScroll2', ['$rootScope', '$timeout', '$interval', function containerScroll2($rootScope, $timeout, $interval) {
    return {
        restrict: "EAC",
        scope: false,
        link: function ($scope, $element, attr) {
            var obj = $element;
            $timeout(function () {
                $rootScope._mainScroll = Math.ceil(obj.context.scrollTop);
            });
            $element.bind('scroll', function () {
                $timeout(function () {
                    $rootScope._mainScroll = Math.ceil(obj.context.scrollTop);
                    if (obj.context.scrollTop == 0) {
                        //$element.next()
                    }
                });
            });
            //鼠标事件
            //鼠标按键按下
            //$element.bind('mousedown', function () {
            //    //alert('mousedown')
            //})
            ////鼠标按键松开
            //$element.bind('mouseup', function () {
            //    //alert('mouseup')
            //})
            //鼠标移动
            //$element.bind('mousemove',function(event){
            //    $timeout(function () {
            //    var e = event || window.event;
            //        $rootScope._x= e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            //        $rootScope._y= e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            //    });
            //})

            //触屏事件
            //当手指放在屏幕上触发
            $element.bind('touchstart', function (event) {
                $timeout(function () {
                    $rootScope._x0 = Math.ceil(event.originalEvent.touches[0].clientX);
                    $rootScope._y0 = Math.ceil(event.originalEvent.touches[0].clientY);
                })
            })
            //当手指在屏幕上滑动时，连续地触发
            $element.bind('touchmove', function (event) {
                $timeout(function () {
                    $rootScope._x = Math.ceil(event.originalEvent.touches[0].clientX);
                    $rootScope._y = Math.ceil(event.originalEvent.touches[0].clientY);
                    //视图滑动切换：暂定80px距离则切换；可改进为计算滑动速度来判断用户是否是用于切换视图
                    if ($rootScope._x > 0) {
                        //向右滑动
                        angular.element(document.querySelector('.container-view')).removeClass('right');
                        angular.element(document.querySelector('.container-view')).addClass('left');
                        angular.element(document.querySelector('.container-view')).css('left', (16 - ($rootScope._x / 40)) + 'rem');
                    } else if ($rootScope._x < 0) {
                        //向左滑动
                        angular.element(document.querySelector('.container-view')).removeClass('left');
                        angular.element(document.querySelector('.container-view')).addClass('right');
                        angular.element(document.querySelector('.container-view')).css('left', (16 + ($rootScope._x / 40)) + 'rem');
                    }
                })
                //可改进为 将触屏记录到一个固定长度数组，不断更新，只用这部分记录用户手指滑动方向
            })
            //当手指从屏幕上离开时触发
            $element.bind('touchend', function () {
                $timeout(function () {
                    var a = Math.abs(($rootScope._y0 - $rootScope._y) / ($rootScope._x - $rootScope._x0));
                    if (a >= 1.73) {
                        //tan(60°) 滑动角度
                        $rootScope._xx = 0;
                        $rootScope._yy = Math.ceil($rootScope._y0 - $rootScope._y);
                    } else if (a <= 0.58) {
                        //tan(30°) 滑动角度
                        $rootScope._xx = Math.ceil($rootScope._x0 - $rootScope._x);
                        $rootScope._yy = 0;
                    } else {
                        $rootScope._xx = Math.ceil($rootScope._x0 - $rootScope._x);
                        $rootScope._yy = Math.ceil($rootScope._y0 - $rootScope._y);
                    }
                    //视图滑动切换：暂定80px距离则切换；可改进为计算滑动速度来判断用户是否是用于切换视图
                    if ($rootScope._x > 80) {
                        //向右滑动
                        angular.element(document.querySelector('.container-view')).removeClass('right');
                        angular.element(document.querySelector('.container-view')).addClass('left');
                        angular.element(document.querySelector('.container-view')).css('left', '14rem');
                    } else if ($rootScope._x < -80) {
                        //向左滑动
                        angular.element(document.querySelector('.container-view')).removeClass('left');
                        angular.element(document.querySelector('.container-view')).addClass('right');
                        angular.element(document.querySelector('.container-view')).css('left', '-14rem');
                    } else {
                        angular.element(document.querySelector('.container-view')).css('left', '16rem');
                    }

                })
            })

        }
    }
}]);